package com.xywl.escort.view.coupon;

import com.xywl.escort.bean.CouponListReq;
import com.xywl.escort.bean.CouponListResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

/**
 * Created by 32422 on 2018/3/7.
 */

public class CouponPresenter implements CouponContract.Presenter {
    private CouponContract.View mView;
    private ApiServer mApiServer;

    @Inject
    CouponPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        this.mView = null;
    }

    @Override
    public void takeView(CouponContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadCouponList(int pageNum, String useScene, int condMinDay, double condMinPrice) {
        CouponListReq req = new CouponListReq();
        req.pageNum = pageNum;
        req.useScene = useScene;
        req.condMinDay = condMinDay;
        req.condMinPriceYuan = condMinPrice;
        mApiServer.listPacke(req)
                .compose(RxUtil.<CouponListResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<CouponListResp>bindToLife())
                .subscribe(new BaseSubscriber<CouponListResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadCouponFailure("");
                    }

                    @Override
                    public void onNext(CouponListResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.updateCoupon(resp.data);
                        } else {
                            mView.loadCouponFailure("");
                        }
                    }
                });
    }

}
